#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/23 20:20
# @Author  : Crissu
# @Site    : 
# @File    : flip.py
# @Software: PyCharm
import multiprocessing, cv2, os

from utils import universal_util
from configs import flip_config


def flipImage(mode=1):
    # 获取当前路径下所有图片名字
    # nameList = universal_util.GetImageSrc(flip_config.FromPath)
    # print(nameList)

    classList = os.listdir(flip_config.FromPath)
    print(classList)

    # 选择模式
    # 0: 需要补边，变成正方形，再resize
    # 1: 需要裁剪长的边跟短边一样长，然后resize
    if mode == 0:
        dealFunc = flip_Horizontal_Vertical0
    if mode == 1:
        dealFunc = flip_Horizontal_Vertical1
    # 创建路径
    universal_util.MakeFloder(flip_config.ToPath)
    pool = multiprocessing.Pool(16)
    pool.map(dealFunc, classList)

def flip_Horizontal_Vertical0(name):
    imgPath = flip_config.FromPath + name
    img = cv2.imread(imgPath)
    path = flip_config.ToPath
    random_name = universal_util.GetRandomName()
    # 水平翻转
    flip_horizontal = cv2.flip(img, 1)
    saveName = "H_" + random_name + "_" + name
    savePath = path + saveName
    cv2.imwrite(savePath, flip_horizontal)
    print("flip_Horizontal_Vertical0 - ", savePath, " - done")
    # 垂直翻转
    # flip_vertical = cv2.flip(img, 0)
    # saveName = "V_" + random_name + "_" + name
    # savePath = path + saveName
    # cv2.imwrite(savePath, flip_vertical)


def flip_Horizontal_Vertical1(className):
    path = flip_config.FromPath + className + "/"
    nameList = os.listdir(path)
    for name in nameList:
        rawPath = path + name
        img = cv2.imread(rawPath)
        # random_name = universal_util.GetRandomName()
        # 水平翻转
        flip_horizontal = cv2.flip(img, 1)
        # saveName = "H_" + random_name + "_" + name
        saveName = "H_" + name
        savePath = flip_config.ToPath + className + "/"
        universal_util.MakeFloder(savePath)
        savePath += saveName
        print(savePath, " - 完毕")
        cv2.imwrite(savePath, flip_horizontal)
        print("flip_Horizontal_Vertical1 - ", savePath, " - done")
        # 垂直翻转
        # flip_vertical = cv2.flip(img, 0)
        # saveName = "V_" + random_name + "_" + name
        # savePath = path + saveName
        # cv2.imwrite(savePath, flip_vertical)








